/*
 * @Description: router index
 * @Author: Rfan
 * @Date: 2021-08-06 11:22:25
 * @LastEditTime: 2023-01-18 18:11:49
 */

import { lazy } from 'react';
import { observer } from 'mobx-react-lite';
import { Navigate, useRoutes } from 'react-router-dom';
import { toJS } from 'mobx';
import { orgNavToRoutes } from '@/utils/router';
import Layout from '@/layout';
import type { IRouteObj } from '@/routes/interface';
import errorRouter from '@/routes/modules/error';
import transferUnitRoutes from './modules/transferUnit';
import LogRoutes from '@/routes/modules/log';
import GlobalPageRoutes from '@/routes/modules/global';
import FullbackRouter from '@/routes/components/FullbackRouter';
import { routerState } from '@/store/routerStore';
import searchRoutes from '@/routes/modules/retrieval';
import PracticeV1 from '@/view/a-practice/Practice';
import PracticeV2 from '@/view/b-practice/Practice';
const PracticeV3 = lazy(() => import('@/view/c-practice/Practice'));
const PracticeV4 = lazy(() => import('@/view/d-practice/Practice'));
const Login = lazy(() => import(/* webpackChunkName: "Login" */ '../view/Login'));
const Welcome = lazy(() => import(/* webpackChunkName: "Welcome" */ '../view/Welcome'));
const Dashboard = lazy(
  () => import(/* webpackChunkName: "Welcome" */ '../view/globalPage/Dashboard')
);
const ChangeLog = lazy(() => import(/* webpackChunkName: "ChangeLog" */ '../view/ChangeLog'));

// const asyncRoutes = orgNavToRoutes(toJS(routerState.asyncRoutes));
const asyncRoutes: any = [];

// @ts-ignore
// const files = require.context('./modules', false, /\.tsx$/);
// console.log('dynamic import', files);

export const staticRoutes: IRouteObj[] = [
  {
    path: '',
    exact: true,
    element: <Navigate to="/login" />
  },
  {
    path: '/login',
    element: <Login />,
    exact: true,
    hidden: true,
    meta: { title: '登录' }
  },
  {
    path: '/changelog',
    element: <ChangeLog />,
    exact: true,
    hidden: true,
    meta: { title: '更新日志' }
  },
  // {
  //   path: '/welcome',
  //   element: <Welcome />,
  //   exact: true,
  //   meta: { title: '欢迎页' }
  // },
  {
    element: <Layout />,
    children: [
      {
        path: '/welcome',
        element: <Welcome />,
        meta: {
          title: '欢迎页'
        }
      },
      {
        path: '/practice-a',
        element: <PracticeV1 />,
        meta: {
          title: '练习页面01'
        }
      },
      {
        path: '/practice-b',
        element: <PracticeV2 />,
        meta: {
          title: '练习页面02'
        }
      },
      {
        path: '/practice-c',
        element: <PracticeV3 />,
        meta: {
          title: '练习页面03'
        }
      },
      {
        path: '/practice-d',
        element: <PracticeV4 />,
        meta: {
          title: '练习页面04'
        }
      },
      {
        path: '/dashboard',
        element: <Dashboard />,
        meta: {
          title: '领导座舱'
        }
      }
    ]
  },
  ...GlobalPageRoutes,
  ...LogRoutes,
  ...transferUnitRoutes,
  ...searchRoutes
];

export const behindRoutes: IRouteObj[] = [
  ...errorRouter,
  {
    path: '*',
    hidden: true,
    element: <FullbackRouter />
  }
];

export const routes: IRouteObj[] = [...staticRoutes, ...asyncRoutes, ...behindRoutes];

const RouteList = () => {
  // 动态路由
  const asyncRoutes: IRouteObj[] = orgNavToRoutes(toJS(routerState.asyncRoutes));
  // @ts-ignore
  return useRoutes([...staticRoutes, ...asyncRoutes, ...behindRoutes]);
};

export default observer(RouteList);
